<template>
  <!-- 试题信息选择模块 -->
  <el-form :model="questionForm" ref="questionRef" label-width="80px">
    <el-row>
      <el-col :span="6">
        <el-form-item label="学科" size="small">
          <el-select 
            @change="subjectChange"
            v-model="questionForm.subjectID"
            placeholder="请选择"
            style="width: 100%">
            <el-option
              v-for="item in subjectOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="二级目录" size="small">
          <el-select v-model="questionForm.catalogID" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in catalogOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="标签" size="small">
          <el-select v-model="questionForm.tags" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in tagOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="关键字" size="small">
          <el-input v-model="questionForm.keyword" placeholder="根据题干搜索"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="试题类型" size="small">
          <el-select v-model="questionForm.questionType" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in quesTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="难度" size="small">
          <el-select v-model="questionForm.difficulty" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in difficultyOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="方向" size="small">
          <el-select v-model="questionForm.direction" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="(item, index) in directionOptions"
              :key="index"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="录入人" size="small">
          <el-select v-model="questionForm.creator" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in creatorOptions"
              :key="item.id"
              :label="item.username"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="题目备注" size="small">
          <el-input v-model="questionForm.remark"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="企业简称" size="small">
          <el-input v-model="questionForm.shortName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="城市" size="small">
          <el-row type="flex" :gutter="6">
            <!-- 城市 -->
            <el-col :span="12">
              <el-select v-model="questionForm.city" @change="cityChange">
                <el-option
                  v-for="(item, index) in cityOptions"
                  :key="index"
                  :label="item"
                  :value="item">
                </el-option>
              </el-select>
            </el-col>
            
            <!-- 地区 -->
            <el-col :span="12">
              <el-select v-model="questionForm.area">
                <el-option
                  v-for="(item, index) in areaOptions"
                  :key="index"
                  :label="item"
                  :value="item">
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item style="text-align: right">
          <el-button size="small" @click="clearForm">清除</el-button>
          <el-button size="small" type="primary" @click="searchQuestion">搜索</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import { simple as getSubjectList } from '@/api/hmmm/subjects'
import { simple as getCatalogList } from '@/api/hmmm/directorys'
import { difficulty, questionType, direction } from '@/api/hmmm/constants'
import { simple as getUserList } from '@/api/base/users'
import { simple as getTags } from '@/api/hmmm/tags'
import { provinces, citys} from '@/api/hmmm/citys'

export default {
  data() {
    return {
      questionForm: {
        subjectID: '',
        catalogID: '',
        tags: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creator: '',
        remarks: '',
        shortName: '',
        city: '',
        area: ''
      },
      subjectOptions: [],
      catalogOptions: [],
      quesTypeOptions: [],
      difficultyOptions: difficulty,
      quesTypeOptions: questionType,
      directionOptions: direction,
      tagOptions: [],
      creatorOptions: [],
      cityOptions: [],
      areaOptions: []
    }
  },
  created() {
    this.getSubjectList()
    this.getUserList()
    this.cityOptions = provinces()
  },
  methods: {
    // 获取学科选项
    async getSubjectList() {
      const res = await getSubjectList()
      this.subjectOptions = res.data
    },
    // 获取二级目录
    async subjectChange() {
      // 根据选择的学科获取二级目录选项
      const res = await getCatalogList({
        subjectID: this.questionForm.subjectID
      })
      this.catalogOptions = res.data
      // 获取标签选项
      const tagsList = await getTags({
        subjectID: this.questionForm.subjectID
      })
      this.tagOptions = tagsList.data
    },
    // 获取录入人选项
    async getUserList() {
      const res = await getUserList()
      this.creatorOptions = res.data
    },
    // 城市变化，获取对应城市的地区
    cityChange() {
      this.areaOptions = citys(this.questionForm.city)
    },
    // 清空表单
    clearForm() {
      this.questionForm = {
        subjectID: '',
        catalogID: '',
        tags: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creator: '',
        remarks: '',
        shortName: '',
        city: '',
        area: ''
      }
    },
    // 传参给父组件，调用搜索接口
    async searchQuestion() {
      this.$emit('search-result', this.questionForm.subjectID, this.questionForm.keyword)
    }
  }
}
</script>

<style></style>
